﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GetTaskClass_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_GetTaskClass_V2];
GO
CREATE PROCEDURE [dbo].[sproc_GetTaskClass_V2]
    @UserName nvarchar(255)
/*

============================================================
功能:    得到@UserName的所有任务
参数:
    @RightCode    int    :    任务代表的权限包ID
============================================================

*/
AS
BEGIN
SET NOCOUNT ON

DECLARE @Staff_id int

SET @staff_id = 0;

--取得用户的ID
SELECT @staff_id = staff_id FROM dbo.UDS_Staff WHERE staff_name = @username;

IF ( @@ROWCOUNT = 1 )
BEGIN
    --得到拥有的类信息
    SELECT
            *,
            (
                SELECT count(*)
                    FROM
                        dbo.UDS_Document
                    WHERE
                        ClassID = c.ClassID
                        and DocApproved = 1
                        and DocType = 0
            ) AS FILECOUNT
        FROM
            dbo.UDS_Class c left outer join dbo.UDS_TeamInfo t on c.ClassID = t.TeamID
        WHERE
            c.ClassID <> c.ClassParentID
            AND c.ClassID in 
            ( 
                SELECT Team_ID FROM dbo.UDS_Staff_In_Team WHERE Staff_id = @staff_id
            ) 
        ORDER BY t.EndDate DESC
END

END
GO